package com.app.nfc;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zndroid.nfc.INFC;
import com.zndroid.nfc.NFC;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private TxtAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initMenu();
        initRecyclerView();

        NFC.getInstance().init(this, new INFC() {
            @Override
            public void onSupport(boolean isSupport) {
                if (!isSupport)
                    NFC.getInstance().openNFCSetting(MainActivity.this);
            }

            @Override
            public void onValid(String SN) {

            }

            @Override
            public void onInvalid() {

            }

            @Override
            public void console(SpannableString txt) {
                adapter.appendTxt(txt);
                if(adapter.getItemCount() > 0) {
                    recyclerView.scrollToPosition(adapter.getItemCount() - 1);
                }
            }
        });
    }

    private void initMenu() {
        findViewById(R.id.clean_btn).setOnClickListener(v -> adapter.clearAll());
    }

    private void initRecyclerView() {
        recyclerView = findViewById(R.id.rv);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        //从下往上刷新数据
        linearLayoutManager.setStackFromEnd(true);
        recyclerView.setLayoutManager(linearLayoutManager);

        adapter = new TxtAdapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        NFC.getInstance().onNewIntent(intent);
    }

    @Override
    protected void onResume() {
        NFC.getInstance().onResume(this);
        super.onResume();
    }

    @Override
    protected void onPause() {
        NFC.getInstance().onPause(this);
        super.onPause();
    }
}
